package com.rui.study.algorithm.L_深入理解JVM_动态方法_应用案例;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.lookup;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-01 16:48
 **/
public class Demo1 {

    class Grandpa {
        public void thinking() {
            System.out.println("Grandpa is thinking!");
        }
    }

    class Father extends Grandpa {
        @Override
        public void thinking() {
            System.out.println("Father is thinking!");
        }
    }

    class Son extends Father {
        @Override
        public void thinking() {
            MethodType mt = MethodType.methodType(void.class);
            try {
                MethodHandle mh = lookup().findSpecial(Grandpa.class, "thinking", mt, getClass());
                mh.invoke(this);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("进入main()");
        Demo1 demo1 = new Demo1();
        demo1.new Son().thinking();
    }

}
